
using System.Web.UI.HtmlControls;
using System;
using System.Configuration;
using System.Drawing;
using System.Data;
using System.Web.UI.WebControls;
using Microsoft.VisualBasic;
using System.Collections;
using System.Web.UI;
using System.Web;


using System.IO;
using System.Text;
//using com.ddsc.database;
using DDSC.OracleADO;
using com.ddsc.common;
using SMAS;
using System.Resources;


namespace smasb2b
{
	public class VNDW0070 : BasePage
	{
		//==================================================================================================
		//程式編號   :           VNDW0070                    版本 :  1.00.00
		//
		//程式名稱   :			 樂e購商品文案維護
		//
		//系統分析師 :           Joe
		//
		//程式設計師 :           Tino
		//
		//編寫日期   :           2013/01/31
		
		//修改記錄   :
		
		//修改案編號      修改日期      修改人員          修改原因            修　改　內　容
		
		//－－－－       －－－－      －－－－     －－－－－－－－       －－－－－－－－－－－－－－－－
		//P3513M0025     20130131      TinoWeng                             Init                    I
		//
		//==================================================================================================
		
#region  Web Form 設計工具產生的程式碼
		
		//此為 Web Form 設計工具所需的呼叫。
		[System.Diagnostics.DebuggerStepThrough()]private void InitializeComponent()
		{
			
		}
		protected ddscControlLibrary.SubmitButtonCtrl btnSearch;
		protected System.Web.UI.WebControls.Label lblSTATUS;
		protected System.Web.UI.WebControls.Label lblPAGEINDEX;
		protected ddscControlLibrary.InputBoxCtrl PageIndex;
		protected System.Web.UI.WebControls.Label lblPAGE1;
		protected System.Web.UI.WebControls.Label lblTOTAL;
		protected ddscControlLibrary.InputBoxCtrl PageCount;
		protected System.Web.UI.WebControls.Label lblPAGE;
		protected System.Web.UI.WebControls.ImageButton btnPREV;
		protected ddscControlLibrary.InputBoxCtrl GotoPage;
		protected System.Web.UI.WebControls.ImageButton BtnGo;
		protected System.Web.UI.WebControls.ImageButton BtnNext;
		protected System.Web.UI.HtmlControls.HtmlImage IMG1;
		protected System.Web.UI.HtmlControls.HtmlTable DataGrid;
		protected System.Web.UI.HtmlControls.HtmlTableRow DataGrid_foot;
		protected System.Web.UI.WebControls.Label lblSMAS_CODE;
		protected ddscControlLibrary.InputBoxCtrl txtSMAS_CODE;
		protected System.Web.UI.WebControls.Label lblB2C_CODE_NAME;
		protected ddscControlLibrary.InputBoxCtrl txtB2C_CODE_NAME;
		protected System.Web.UI.WebControls.Label lblB2C_CODE;
		protected System.Web.UI.WebControls.Label lblB2C_CODE_NAME1;
		protected System.Web.UI.WebControls.Label lblDOC_EFF_DATE;
		protected System.Web.UI.WebControls.Label lblDOC_EXP_DATE;
		protected System.Web.UI.WebControls.Label lblDOC_VER;
		protected System.Web.UI.WebControls.Label lblVNDW0070;
		
		//注意: 下列預留位置宣告是 Web Form 設計工具需要的項目。
		//請勿刪除或移動它。
		private System.Object designerPlaceholderDeclaration;
		
		private void Page_Init(System.Object sender, System.EventArgs e)
		{
			//CODEGEN: 此為 Web Form 設計工具所需的方法呼叫
			//請勿使用程式碼編輯器進行修改。
			InitializeComponent();
		}
		
#endregion
		
		private DataTable createErrorTable(string mError)
		{
			dbReturnInfo dbInfo = new dbReturnInfo("info");
			dbInfo.AddNewRow();
			dbInfo.setReturnDataValue("F", System.Convert.ToString(999), 0, mError);
			return dbInfo.getReturnDataTable();
		}


        public void DataGrid_DataBind()
        {
            //變數宣告
            VENDOR LoginUser = GetLoginUser();
            DataSet ds = default(DataSet);
            dbAccess dba = new dbAccess();
            ArrayList m_arrParam = new ArrayList();
            string strCondition = "";
            int i = 0;
            int count = 1;
            string[] str_SQL_a = new string[count + 1];
            string[] str_SQL_b = new string[count + 1];

            try
            {
                //目前頁次指定Goto
                GotoPage.Text = (dynamic)PageIndex.Text;

                //預設查詢語法
                //P3511M0122-報價呈案程式修改案
                //Dim SqlCommandText = "select * from (select A.*,  (decode(b.status,'8','8-報價中','2','2-已報價','1','','')) status from (select A.CODE CODE3,B.ENGLISH_SHORTNAME,A.SUPPLY_PRICE from VND_SUPPLY A,BAS_MATERIAL B where A.code=B.code(+)  AND A.VENDOR_NO='" & LoginUser.VENDOR_NO & "') A ,B2B_SUPPLY B where A.CODE3=B.CODE(+) ) "

                //Tony: 1_1.修改SQL語法使用參數，不組字串
                string SqlCommandText = "SELECT A2.B2C_CODE AS B2C_CODE,A2.B2C_CODE_NAME AS B2C_CODE_NAME1,Decode(NVL(B2.STATUS ,\'\'),9, NVL(B2.DOC_EFF_DATE ,\'\')) AS DOC_EFF_DATE,Decode(NVL(B2.STATUS ,\'\'),9,NVL(B2.DOC_EXP_DATE ,\'\')) AS DOC_EXP_DATE,(CASE WHEN NVL(B2.DOC_DISABLE,\'\') = \'Y\' AND NVL(B2.STATUS ,\'\') = 9 THEN \'已審核(停用)\' ELSE Decode(NVL(B2.STATUS ,\'\'),\'\',\'\',\'1\',\'已存檔\',\'2\',\'審核中\',\'3\',\'審核中\',\'9\',\'已審核\') END) AS STATUS,DECODE(NVL(B2.DOC_VER ,\'\'),\'\',\'\',TO_CHAR(B2.DOC_VER,\'0999\')) AS DOC_VER, DECODE(DECODE(NVL(B2.STATUS ,\'\'),\'\',\'1\'),\'1\',\'10\',B2.STATUS) TEMPSTATUS  FROM (SELECT Distinct A.B2C_CODE,A.B2C_CODE_NAME FROM B2C_CODE_MAPPING_MASTER A ,B2C_CODE_MAPPING_DETAILS B,BAS_MATERIAL C WHERE A.B2C_CODE = B.B2C_CODE AND B.SMAS_CODE = C.CODE(+) AND C.VENDOR_NO = :LoginUser {0}  AND A.B2C_CODE NOT IN (SELECT A1.B2C_CODE FROM (Select C2.B2C_CODE FROM (SELECT COUNT(A.B2C_CODE) as B2C_COUNT, A.B2C_CODE FROM B2C_CODE_MAPPING_MASTER A ,B2C_CODE_MAPPING_DETAILS B,BAS_MATERIAL C WHERE A.B2C_CODE = B.B2C_CODE AND B.SMAS_CODE = C.CODE(+) GROUP BY A.B2C_CODE) C2 WHERE C2.B2C_COUNT >1) A1 ,B2C_CODE_MAPPING_DETAILS B1,BAS_MATERIAL C1 WHERE A1.B2C_CODE = B1.B2C_CODE AND B1.SMAS_CODE = C1.CODE(+) AND C1.VENDOR_NO != :LoginUser)) A2, B2C_WEB_CONTENT B2 WHERE A2.B2C_CODE = B2.CODE(+)";
                //string SqlCommandText = "SELECT A2.B2C_CODE AS B2C_CODE,A2.B2C_CODE_NAME AS B2C_CODE_NAME1,Decode(NVL(B2.STATUS ,\'\'),9, NVL(B2.DOC_EFF_DATE ,\'\')) AS DOC_EFF_DATE,Decode(NVL(B2.STATUS ,\'\'),9,NVL(B2.DOC_EXP_DATE ,\'\')) AS DOC_EXP_DATE,(CASE WHEN NVL(B2.DOC_DISABLE,\'\') = \'Y\' AND NVL(B2.STATUS ,\'\') = 9 THEN \'已審核(停用)\' ELSE Decode(NVL(B2.STATUS ,\'\'),\'\',\'\',\'1\',\'已存檔\',\'2\',\'審核中\',\'3\',\'審核中\',\'9\',\'已審核\') END) AS STATUS,DECODE(NVL(B2.DOC_VER ,\'\'),\'\',\'\',TO_CHAR(B2.DOC_VER,\'0999\')) AS DOC_VER, DECODE(DECODE(NVL(B2.STATUS ,\'\'),\'\',\'1\'),\'1\',\'10\',B2.STATUS) TEMPSTATUS  FROM (SELECT Distinct A.B2C_CODE,A.B2C_CODE_NAME FROM B2C_CODE_MAPPING_MASTER A ,B2C_CODE_MAPPING_DETAILS B,BAS_MATERIAL C WHERE A.B2C_CODE = B.B2C_CODE AND B.SMAS_CODE = C.CODE(+) AND C.VENDOR_NO = :LoginUser :strSMAS_CODE  AND A.B2C_CODE NOT IN (SELECT A1.B2C_CODE FROM (Select C2.B2C_CODE FROM (SELECT COUNT(A.B2C_CODE) as B2C_COUNT, A.B2C_CODE FROM B2C_CODE_MAPPING_MASTER A ,B2C_CODE_MAPPING_DETAILS B,BAS_MATERIAL C WHERE A.B2C_CODE = B.B2C_CODE AND B.SMAS_CODE = C.CODE(+) GROUP BY A.B2C_CODE) C2 WHERE C2.B2C_COUNT >1) A1 ,B2C_CODE_MAPPING_DETAILS B1,BAS_MATERIAL C1 WHERE A1.B2C_CODE = B1.B2C_CODE AND B1.SMAS_CODE = C1.CODE(+) AND C1.VENDOR_NO != :LoginUser)) A2, B2C_WEB_CONTENT B2 WHERE A2.B2C_CODE = B2.CODE(+)";

                string strSMAS_CODE = string.Empty;

                if (txtSMAS_CODE.Text.Replace(",", "\'\'") != "")
                {
                    //strSMAS_CODE = " AND B.SMAS_CODE LIKE\'%" + txtSMAS_CODE.Text.Replace(",", "\'\'") + "%\'";
                    strSMAS_CODE = " AND B.SMAS_CODE LIKE :strSMAS_CODE";
                    m_arrParam.Add(dba.GetOracleParam(":strSMAS_CODE", "%" + txtSMAS_CODE.Text.Replace(",", "\'\'") + "%"));
                }

                //Tony : 2_1.設定動態參數物件
                SqlCommandText = string.Format(SqlCommandText, strSMAS_CODE);
                m_arrParam.Add(dba.GetOracleParam(":LoginUser", LoginUser.VENDOR_NO));
                //m_arrParam.Add(dba.GetOracleParam(":strSMAS_CODE", strSMAS_CODE));


                //組合條件字串
                str_SQL_a[0] = txtB2C_CODE_NAME.Text.Replace(",", "\'\'");

                //Tony:1_2修改SQL語法使用參數，不組字串
                //str_SQL_b[0] = string.Format("B2C_CODE_NAME like \'{0}%\' and ", str_SQL_a[0]);
                str_SQL_b[0] = "B2C_CODE_NAME like :SQLA0 and ";

                //Tony : 2_2.設定動態參數物件
                if (str_SQL_a[0].Trim() != "")
                    m_arrParam.Add(dba.GetOracleParam(":SQLA0", "%" + str_SQL_a[0] + "%"));

                //查詢條件組合
                for (i = 0; i <= count - 1; i++)
                {
                    if (str_SQL_a[i].Trim() != "")
                    {
                        strCondition = strCondition + str_SQL_b[i];
                    }
                }

                if (!(String.IsNullOrEmpty(strCondition)))
                {
                    strCondition = " AND " + strCondition.Substring(0, strCondition.Length - 5);
                    // strCondition = " where VENDOR_NO='" & LoginUser.VENDOR_NO & "'" + strCondition.Substring(0, strCondition.Length - 3)

                }
                SqlCommandText = SqlCommandText + strCondition;
                int intCount = 0;
                int size = PageSize; // Update by titan 由BasePage設定

                string SqlCommandCount = "SELECT count(*) row_total_count from (" + SqlCommandText + ") ";

                // 計算資料總筆數
                //Tony : 3.修改執行方法Overload
                //DataSet dsCount = dba.runExecuteQuery("Vendor_Count", SqlCommandCount);
                DataSet dsCount = dba.runExecuteQuery("Vendor_Count", SqlCommandCount.ToString(), m_arrParam);

                if (dsCount.Tables[0].Rows[0]["ReturnCode"] == "000")
                {
                    intCount = int.Parse(System.Convert.ToString(dsCount.Tables["Vendor_Count"].Rows[0]["row_total_count"]));
                    PageCount.Text = System.Convert.ToString(Math.Ceiling(System.Convert.ToDecimal(double.Parse((intCount).ToString()) / size)));
                }

                //Update By Titan 處理當總頁次小於目前頁次
                if ((int.Parse(PageCount.Text) < int.Parse(PageIndex.Text)) && int.Parse(PageCount.Text) > 0)
                {
                    // 總筆數(intCount)小於一頁，從第0筆開始顯示
                    PageIndex.Text = (dynamic)PageCount.Text;
                    GotoPage.Text = (dynamic)PageCount.Text;
                }
                // Update by titan 由BasePage設定
                // absolute position
                int position = ((int.Parse(PageIndex.Text)) - 1) * size;
                int maxrecord = position + size;


                //控制分頁按鈕
                ControlPageButton(DataGrid_foot);



                //執行資料庫語法
                //Tony : 3.修改執行方法Overload
                //ds = dba.runExecuteQuery("VND_SUPPLY", SqlCommandText + " ORDER BY B2C_CODE ", maxrecord, position);
                ds = dba.runExecuteQuery("VND_SUPPLY", SqlCommandText + " ORDER BY B2C_CODE ", position, maxrecord, m_arrParam);

                if (!(ds == null) && ds.Tables["info"].Rows[0]["ReturnCode"] != "100" && ds.Tables["info"].Rows[0]["ReturnCode"] != "999")
                {
                    HtmlTableRow tr = (HtmlTableRow)(DataGrid.FindControl("DataGrid_head"));
                    ArrayList ArrKey = new ArrayList();
                    ArrKey.Add("B2C_CODE");
                    ArrKey.Add("TEMPSTATUS");
                    ArrKey.Add("DOC_VER");
                    //ArrKey.Add("B2C_CODE_NAME1")
                    DataGrid_Bind_VNDW0070(DataGrid, ArrKey, ds.Tables["VND_SUPPLY"]);

                }
                else
                {
                    ResourceManager rm = GetResourceManager();
                    showException(rm.GetString("MSG069"));
                }

            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                ds.Dispose();
            }
        }
		
		private void BtnNext_Click(System.Object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				PageIndex.Text = ((int.Parse(PageIndex.Text)) + 1).ToString();
				DataGrid_DataBind();
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
		}
		
		private void btnPREV_Click(System.Object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				PageIndex.Text = ((int.Parse(PageIndex.Text)) - 1).ToString();
				DataGrid_DataBind();
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
			
		}
		
		private void BtnGo_Click(System.Object sender, System.Web.UI.ImageClickEventArgs e)
		{
			ResourceManager rm = GetResourceManager();
			try
			{
                PageIndex.Text = (dynamic)GotoPage.Text;
				DataGrid_DataBind();
			}
			catch (Exception)
			{
				showException(rm.GetString("MSG057"));
			}
			
		}
		
		private void btnSearch_Click(System.Object sender, System.EventArgs e)
		{
			try
			{
				PageIndex.Text = System.Convert.ToString(1);
				DataGrid_DataBind();
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
			
		}
		
		private void Page_Load(System.Object sender, System.EventArgs e)
		{
			//在這裡放置使用者程式碼以初始化網頁
			try
			{
				
				if (!Page.IsPostBack)
				{
					
				}
				else
				{
					if (IsReSubmit)
					{
						DataGrid_DataBind();
					}
				}
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
            this.BtnNext.Click += BtnNext_Click;
            this.BtnGo.Click += BtnGo_Click;
            this.btnPREV.Click += btnPREV_Click;
            this.btnSearch.Click += btnSearch_Click;
			
		}
		
		
		//將資料呈現於畫面
		public void DataGrid_Bind_VNDW0070(HtmlTable DataGrid, ArrayList ArrKey, DataTable DataSource)
		{
			
			HtmlTableRow tr = (HtmlTableRow) (DataGrid.FindControl("DataGrid_head"));
			int i = 0;
			int j = 0;
			int k = 0;
			string Url = "";
			string className = "";
			ResourceManager rm = GetResourceManager();
			try
			{
				for (i = 0; i <= DataSource.Rows.Count - 1; i++)
				{
					HtmlTableRow htr = new HtmlTableRow();
					if (i % 2 == 0)
					{
						className = "evenRow";
					}
					else
					{
						className = "oddRow";
					}
					htr.Attributes.Add("class", className);
					DataGrid.Rows.Add(htr);
					string strTempStatus = DataSource.Rows[i]["tempstatus"].ToString();
					if (strTempStatus.Length != 0)
					{
						switch (strTempStatus)
						{
							case "1": //已存檔
								tr.Cells[0].Attributes["class"] = "else";
								tr.Cells[1].Attributes["class"] = "link";
								tr.Cells[2].Attributes["class"] = "link";
								tr.Cells[3].Attributes["class"] = "link";
								break;
							case "2": //審核中
							case "3":
								tr.Cells[0].Attributes["class"] = "else";
								tr.Cells[1].Attributes["class"] = "else";
								tr.Cells[2].Attributes["class"] = "link";
								tr.Cells[3].Attributes["class"] = "else";
								break;
							case "9": //已審核
								tr.Cells[0].Attributes["class"] = "link";
								tr.Cells[1].Attributes["class"] = "else";
								tr.Cells[2].Attributes["class"] = "link";
								tr.Cells[3].Attributes["class"] = "else";
								break;
							case "10": //沒資料
								tr.Cells[0].Attributes["class"] = "link";
								tr.Cells[1].Attributes["class"] = "else";
								tr.Cells[2].Attributes["class"] = "else";
								tr.Cells[3].Attributes["class"] = "else";
								break;
						}
					}
					
					
					for (j = 0; j <= tr.Cells.Count - 1; j++)
					{
						HtmlTableCell hd = new HtmlTableCell();
						
						switch (tr.Cells[j].Attributes["class"].ToLower())
						{
							case "link":
								Url = string.Format("{0}?action_type={1}&GUID={2}", tr.Cells[j].Attributes["url"].ToString(), tr.Cells[j].ID, Guid.NewGuid());
								for (k = 0; k <= ArrKey.Count - 1; k++)
								{
									Url += "&" + ArrKey[k] + "=" + System.Web.HttpUtility.UrlEncode(DataSource.Rows[i][ArrKey[k].ToString()].ToString(), System.Text.Encoding.GetEncoding("UTF-8")).Trim().Replace("+", "");
								}
								
								
								hd.Width = tr.Cells[j].Width;
								
								if (tr.Cells[j].ID == "ADD" && strTempStatus == "9")
								{
									string strFormat = "<a href=\"#\" onclick=\"return AddWithSave(\'" + Url + ("\')\">" + rm.GetString(tr.Cells[j].ID) + "</a>");
									hd.InnerHtml = strFormat;
								}
								else
								{
									
									hd.InnerHtml = string.Format("<a href=\"javascript:window.showModalDialog(\'{0}\',this,\'{1}\', true)\">{2}</a>", Url, "dialogWidth=800px;dialogHeight=740px;center=yes;border=thin;help=no;status=no;resizable=no;", rm.GetString(tr.Cells[j].ID));
								}
								break;
								
							case "tdtitle":
								hd.Width = tr.Cells[j].Width;
								hd.Attributes.Add("class", "detail");
								hd.InnerText = System.Convert.ToString(DataSource.Rows[i][tr.Cells[j].Controls[1].ID.Replace("lbl", "")].ToString());
								break;
							default:
								hd.Width = tr.Cells[j].Width;
								if (tr.Cells[j].ID != null)
								{
									hd.InnerText = rm.GetString(tr.Cells[j].ID);
								}
								break;
								
						}
						DataGrid.Rows[DataGrid.Rows.Count - 1].Cells.Add(hd);
						
					}
				}
			}
			catch (Exception ex)
			{
				throw (ex);
			}
			
			
		}
		
		
	}
	
}
